//
//  ASAvatarGroupTagView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/29.
//

#import "ASAvatarGroupTagView.h"

static NSString *ASAvatarGroupTagCellStr = @"ASAvatarGroupTagCell";

@interface ASAvatarGroupTagCell : UICollectionViewCell

@property (strong, nonatomic) UILabel *mainLab;
@property (strong, nonatomic) UIView *mainView;
@property (strong, nonatomic) UIImageView *leftImage;

@end

@implementation ASAvatarGroupTagCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.mainView];
        [self.mainView addSubview:self.leftImage];
        [self.mainView addSubview:self.mainLab];
        
        @weakify(self);
        [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
        [self.leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.centerY.mas_equalTo(self.mainView.mas_centerY);
            make.left.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(10, 11));
        }];
        
        [self.mainLab mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.left.mas_equalTo(self.leftImage.mas_right).mas_offset(5);
            make.centerY.mas_equalTo(self.mainView.mas_centerY);
        }];
    }
    return self;
}

- (UIView *)mainView{
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _mainView.layer.masksToBounds = YES;
        _mainView.layer.cornerRadius = 12.5;
    }
    return _mainView;
}

- (UIImageView *)leftImage{
    if (!_leftImage) {
        _leftImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"details_bq"]];
    }
    return _leftImage;
}

- (UILabel *)mainLab{
    if (!_mainLab) {
        _mainLab = [UILabel new];
        _mainLab.font = [UIFont systemFontOfSize:12];
        _mainLab.textColor = [UIColor colorWithHexString:@"#808080"];
    }
    return _mainLab;
}

@end

@implementation ASAvatarGroupTagView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
//        UICollectionViewLeftAlignedLayout *fl = [UICollectionViewLeftAlignedLayout new];
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        fl.minimumLineSpacing = 9;
        fl.minimumInteritemSpacing = 9;
        fl.sectionInset = UIEdgeInsetsMake(0, 14, 0, 14);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30) collectionViewLayout:fl];
        _collectionView.backgroundColor = self.backgroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = NO;
        _collectionView.alwaysBounceHorizontal = YES;
        [_collectionView registerClass:[ASAvatarGroupTagCell class] forCellWithReuseIdentifier:ASAvatarGroupTagCellStr];
    }
    return _collectionView;
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        NSString *model = self.dataArr[indexPath.row];
        CGFloat w = [ASTool getWidthWithText:model height:25 font:12];
        return CGSizeMake(w+25+11, 25);
    }else{
        return CGSizeMake(100, 25);
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASAvatarGroupTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASAvatarGroupTagCellStr forIndexPath:indexPath];
    if (self.dataArr.count > indexPath.row) {
        NSString *model = self.dataArr[indexPath.row];
        cell.mainLab.text = model;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > indexPath.row) {
        NSString *model = self.dataArr[indexPath.row];
        if (self.clickSubTagBlock) {
            self.clickSubTagBlock(model);
        }
    }
}


@end
